﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace MyApis.Controllers
{
    public class TestController : Controller
    {
        public IActionResult Index(string v="")
        {
            var str = v + DateTime.Now + "-1111";
            return new ContentResult{ 
                Content = str
            };
        }

        public JsonResult Env([FromServices] IHostingEnvironment env, [FromServices] IConfiguration cfg)
        {
            var envData = new {
                env.ApplicationName,
                env.ContentRootPath,
                env.EnvironmentName,
                IsDevelopment = env.IsDevelopment(),
                IsProduction = env.IsProduction()
            };

            var a = cfg["mykey"];

            var cfgData = new Dictionary<string, object>(){
                { "mykey",cfg["mykey"] },
                { "myDeepKey:key1", cfg["myDeepKey:key1"]},
                { "myDeepKey:key2:deep", cfg["myDeepKey:key2:deep"]},
                { "myDeepKey:keyArr:0:val", cfg["myDeepKey:keyArr:0:val"]},
                { "myDeepKey:keyArr:1:val", cfg["myDeepKey:keyArr:1:val"]},
            };

            var jsonSetting = new Newtonsoft.Json.JsonSerializerSettings
            {

            };

            var allEnvData = System.Environment.GetEnvironmentVariables() as System.Collections.Hashtable;

            var allEnv = new Dictionary<string, object>();
            
            foreach(var item in allEnvData.Keys)
            {
                allEnv.Add(item.ToString(), allEnvData[item]);
            }
            
            
            jsonSetting.Formatting = Newtonsoft.Json.Formatting.Indented;


            return new JsonResult(new { envData, cfgData, allEnv }, jsonSetting);
        }
    }
}